<template>
  <!-- 模板部分不变，无需修改 -->
  <view class="reset-password-page">
    <view class="reset-card">
      <view class="title-wrapper">
        <text class="title">重置密码</text>
        <text class="subtitle">设置新密码以保护账号安全</text>
      </view>
      <view class="input-group">
        <view class="input-item">
          <text class="input-icon">📱</text>
          <input 
            v-model="phone" 
            type="number" 
            maxlength="11" 
            placeholder="请输入注册手机号" 
            class="input-content"
          />
        </view>
        <view class="input-item">
          <text class="input-icon">🔒</text>
          <input 
            v-model="newPwd" 
            password
            maxlength="20" 
            placeholder="请设置新密码（≥6位）" 
            class="input-content"
          />
        </view>
      </view>
      <text v-if="newPwd.length >= 6" class="pwd-strength" :style="{ color: getPwdColor() }">
        {{ getPwdStrength() }}
      </text>
      <button 
        class="reset-btn" 
        @click="doReset"
        :disabled="!phone || newPwd.length < 6"
      >
        确认重置
      </button>
      <text v-if="tip" class="tip">{{ tip }}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      phone: '',
      newPwd: '',
      tip: ''
    }
  },
  methods: {
    goBack() {
      uni.navigateBack({ delta: 1 });
    },
    getPwdColor() {
      const pwd = this.newPwd;
      if (/[A-Z]/.test(pwd) && /[a-z]/.test(pwd) && /\d/.test(pwd)) return '#00b42a';
      if ((/[A-Za-z]/.test(pwd) && /\d/.test(pwd)) || (/[A-Z]/.test(pwd) && /[a-z]/.test(pwd))) return '#ff7d00';
      return '#fa5151';
    },
    getPwdStrength() {
      const pwd = this.newPwd;
      let strength = '弱';
      if (/[A-Z]/.test(pwd) && /[a-z]/.test(pwd) && /\d/.test(pwd)) strength = '强';
      else if ((/[A-Za-z]/.test(pwd) && /\d/.test(pwd)) || (/[A-Z]/.test(pwd) && /[a-z]/.test(pwd))) strength = '中';
      return `密码强度：${strength}`;
    },
    doReset() {
      // 1. 简单校验
      if (!/^1[3-9]\d{9}$/.test(this.phone.trim())) {
        this.tip = '手机号格式错误';
        return;
      }
      if (this.newPwd.length < 6) {
        this.tip = '密码至少 6 位';
        return;
      }
      this.tip = '';
    
      // 2. 表单方式提交（和后端 @RequestParam 对应）
      uni.request({
        url: 'http://localhost:8080/api/student/forgot-password',
        method: 'POST',
        header: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: {
          phone: this.phone.trim(),      // 必须叫 phone
          newPassword: this.newPwd       // 必须叫 newPassword
        },
        success: (res) => {
          if (res.data.code === 200) {
            uni.showToast({ title: '密码重置成功', icon: 'success' });
            setTimeout(() => uni.navigateBack(), 800);
			uni.reLaunch({ url: '/pages/login/index' });
          } else {
            this.tip = res.data.message || '重置失败';
          }
        },
        fail: () => (this.tip = '网络错误')
      });
    }
  }
}
</script>

<style scoped>
/* 样式部分不变，无需修改 */
.reset-password-page {
  background-color: #f5f7fa;
  min-height: 100vh;
  padding: 30rpx 20rpx;
  box-sizing: border-box;
}
.back-icon {
  margin-bottom: 40rpx;
  cursor: pointer;
}
.back-text {
  font-size: 32rpx;
  color: #4749a1;
  font-weight: 500;
}
.reset-card {
  background: #ffffff;
  border-radius: 30rpx;
  padding: 60rpx 40rpx;
  box-shadow: 0 15rpx 40rpx rgba(0, 0, 0, 0.05);
  position: relative;
  overflow: hidden;
}
.reset-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 8rpx;
  background: linear-gradient(90deg, #4749a1, #6c6edb);
}
.title-wrapper {
  text-align: center;
  margin-bottom: 60rpx;
}
.title {
  font-size: 44rpx;
  color: #333333;
  font-weight: 600;
  margin-bottom: 15rpx;
  display: block;
}
.subtitle {
  font-size: 26rpx;
  color: #999999;
  display: block;
}
.input-group {
  margin-bottom: 40rpx;
}
.input-item {
  display: flex;
  align-items: center;
  height: 100rpx;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 10rpx;
  padding-left: 10rpx;
}
.input-icon {
  margin-right: 20rpx;
  font-size: 32rpx;
  color: #4749a1;
  width: 32rpx;
  text-align: center;
}
.input-content {
  flex: 1;
  height: 100%;
  font-size: 32rpx;
  color: #333333;
  padding: 0;
  border: none;
  outline: none;
}
.input-content::placeholder {
  color: #cccccc;
  font-size: 30rpx;
}
.pwd-strength {
  display: block;
  text-align: right;
  font-size: 24rpx;
  margin-bottom: 30rpx;
}
.reset-btn {
  width: 100%;
  height: 96rpx;
  background: linear-gradient(90deg, #4749a1, #6c6edb);
  color: #ffffff;
  border-radius: 48rpx;
  font-size: 34rpx;
  font-weight: 500;
  margin-bottom: 30rpx;
  border: none;
}
.reset-btn:disabled {
  background: #e0e0e0;
  color: #ffffff;
  opacity: 0.8;
}
.tip {
  display: block;
  text-align: center;
  color: #fa5151;
  font-size: 26rpx;
  margin-top: 20rpx;
  line-height: 1.5;
}
</style>